package org.benetech.collections;

import java.util.HashMap;
import java.util.Map;

/**
 * Keeps track of multiple totals.
 * @author Reuben Firmin
 */
public class Counter {


	private Map<String, Integer> countMap = new HashMap<String, Integer>();


	/**
	 * Default Constructor.
	 */
	public Counter() {
	}

	/**
	 * Increment the given total.
	 * @param name The name of the total
	 * @return never null
	 */


	public final int increment(final String name) {
		Integer total = countMap.get(name);
		if (total == null) {
			total = Integer.valueOf(1);
		} else {
			total = Integer.valueOf(total + 1);
		}
		countMap.put(name, total);
		return total;
	}

	/**
	 * Return the total of the given type. Will return zero if not found.
	 * @param name The name of the total
	 * @return never null
	 */
	public final int getTotal(final String name) {
		if (countMap.get(name) == null) {
			return 0;
		}
		return countMap.get(name);
	}


}
